package g11.dao;

import g11.model.Drug;
import g11.model.Prescription;
import g11.model.VisitationContent;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class PrescriptionDAOImpl implements PrescriptionDAO {

	private Session session;

	public Session getSession() {
		return this.session;
	}

	public void cleanup() {
		if (session != null)
			session.close();
	}

	public PrescriptionDAOImpl() {

	}

	public void createPrescription(Prescription prescription)
			throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.save(prescription);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}

	}

	public void createPrescription(VisitationContent visitationContent,
			Drug drug, String dosage) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Prescription tempPrescription = new Prescription();
		tempPrescription.setDosage(dosage);
		tempPrescription.setDrugId(drug.getDrugId());
		tempPrescription.setVisitationContentId(visitationContent
				.getVisitationContentId());
		tempPrescription.setDrug(drug);
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.save(tempPrescription);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}

	}

	public void updatePrescription(Prescription prescription)
			throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.update(prescription);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
	}

	public void deletePrescription(Prescription prescription)
			throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.delete(prescription);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}

	}

	public List<Prescription> getPrescriptionByVisitationContent(
			VisitationContent visitationContent) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		List<Prescription> prescriptionList = new ArrayList<Prescription>();
		try {
			String hql = "from Prescription p where p.visitationContentId=?";
			Query q = session.createQuery(hql);
			q.setInteger(0, visitationContent.getVisitationContentId());
			if (q.list().size() > 0) {
				prescriptionList = (List<Prescription>) q.list();
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return prescriptionList;

	}

	public Prescription getPrescriptionByVisitationContentAndDrug(
			VisitationContent visitationContent, Drug drug) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Prescription prescription = new Prescription();
		try {
			String hql = "from Prescription p where p.visitationContentId=? and p.drugId=?";
			Query q = session.createQuery(hql);
			q.setInteger(0, visitationContent.getVisitationContentId());
			q.setInteger(1, drug.getDrugId());
			if (q.list().size() > 0) {
				prescription = (Prescription) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}

		return prescription;
	}

}
